import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class node{
    int x;
    int y;
    char num;
}
public class demo07 {
    //长草
    public static int[] dx={0,1,0,-1};
    public static int[] dy={1,0,-1,0};
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        String[] arr1=new String[n];
        char[][] arr=new char[n][m];
        for (int i = 0; i < n; i++) {
            arr1[i]=sc.next();
            arr[i]=arr1[i].toCharArray();
        }
        int k=sc.nextInt();
        Queue<node> queue=new LinkedList<>();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if(arr[i][j]=='g'){
                    node tmp=new node();
                    tmp.x=i;
                    tmp.y=j;
                    tmp.num=arr[i][j];
                    queue.offer(tmp);
                }
            }
        }
        while (!queue.isEmpty()&&k>0){
            int size=queue.size();
            while (size>0){
                node tmp=queue.poll();
                int i=tmp.x;
                int j=tmp.y;
                for (int l = 0; l < 4; l++) {
                    int i1=i+dx[l];
                    int j1=j+dy[l];
                    if(i1>=0&&i1<n&&j1>=0&&j1<m&&arr[i1][j1]!='g'){
                        node tmp1=new node();
                        tmp1.x=i1;
                        tmp1.y=j1;
                        tmp1.num='g';
                        arr[i1][j1]=tmp1.num;
                        queue.offer(tmp1);
                    }
                }
                size--;
            }
            k--;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print(arr[i][j]);
            }
            System.out.println();
        }
    }
}
